Javascript @ server- Node.js JavaScript is a “complete” language.In mid-90′s Javascript used as DHTML helper.And then the role of javascript increased and form the more serious frontend stuff like jQuery.Now we are discussing about how to use Javascript at the server-side.Node.js really is just another context: it allows you to run JavaScript code in the backend, outside a browser.In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google’s V8 VM, the same runtime environment for JavaScript that Google Chrome uses.We can find a lot of useful modules for Node.js. Thus, Node.js is really two things: a runtime environment and a library. What is Node.Js Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. ie We can use Node.js for developing applications that make heavy use of the ability to run JavaScript both on the client, as well as on server side and therefore benefit from the re-usability of code and the lack of context switching.It is a best example of an “event-driven” system. A software architecture that’s built around not threads or data but events: messages from other applications or inputs from users.Apart from other server side coding laguages like PHP,Ruby,Java Node.js doesn’t wait for one thing to happen before moving to the next.Node is built atop Google’s V8 JavaScript Engine, the open source JavaScript engine at the heart of Google’s Chrome browser
event-based non-blocking I/O model lightweight and efficient asynchronous I/O framework Applications that can be written using Node.js includes
Web Application frameworks Static file servers Servers for HTML5 multi player games Messaging middleware Installation Official installation instructions: https://github.com/joyent/node/wiki/Installation
Hello World
Create a file helloworld.js.And wite the following line of codes
var http = require(“http”);
http.createServer(function(request, response) { response.writeHead(200, {“Content-Type”: “text/plain”}); response.write(“Hello World”); response.end(); }).listen(3000);
Now in browser go to the url http://localhost:3000/
Now I will explain what is meant by each of these statement The first line var http = require(“http”); requires the http module.We reffer that module using http variable in our code. We then call one of the functions the http module offers: createServer. This function returns an object, and this object has a method named listen, and takes a numeric value which indicates the port number our HTTP server is going to listen on.
Now we can take a look at the Node.js event loop. Consider the following code:
var result = database.query(“SELECT * FROM hugetable”);
console.log(“Hello World”); Remember that in Node.js there is only one single process. If there is a slow database query somewhere in this process, this affects the whole process – everything comes to a halt until the slow query has finished. So in Node.js introduce the concept of event-driven, asynchronous callbacks, by utilizing an event loop. So in the following code
database.query(“SELECT * FROM hugetable”, function(rows) { var result = rows; }); console.log(“Hello World”);
Instead of expecting database.query() to directly return a result to us, we pass it a second parameter, an anonymous function. Then, it immediately executes console.log(), and afterwards, it enters the event loop. Node.js continuously cycles through this loop again and again whenever there is nothing else to do, waiting for events. Events like, e.g., a slow database query finally delivering its results.
Note that if we execute the corresponding code written in PHP, it will read the complete result set from the database, and then it can execute the console.log() function.Moreover web server starts its own PHP process for every HTTP request it receives.And if one of these requests results in the execution of a slow piece of code, it results in a slow page load for this particular user, but other users requesting other pages would not be affected.
References:-
official API documentation http://nodejs.org/docs/latest/api/index.html Node Packaged Modules https://npmjs.org/ Total Packages: 28 840 Official GitHub Site https://github.com/joyent/node Official GitHub Wiki – Containing a great amount of links to Node.js based projects https://github.com/joyent/node/wiki Official Installation Guide https://github.com/joyent/node/wiki/Installation http://stackoverflow.com/tags/node.js/info